package com.liu.dao;

import com.liu.entity.T_user;

import java.sql.*;

public class T_userDaoImpl extends BaseDao implements  T_userDao{
    @Override
    public int add(T_user user) {
        int ret=0;
        Connection con=getConnection();
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
//             id,  account,  nick_name,  password,  gender,  id_card_no,  email,  phone,  type,  open_i
            //编写sql
            String sql="INSERT into t_user(account,  nick_name,  password,  gender,  id_card_no,  email," +
                    " phone) VALUES (?,?,?,?,?,?,?)";

            pstm=con.prepareStatement(sql);

            pstm.setString(1,user.getAccount());
            pstm.setString(2,user.getNick_name());
            pstm.setString(3,user.getPassword());
            pstm.setInt(4,user.getGender());
            pstm.setString(5,user.getId_card_no());
            pstm.setString(6,user.getEmail());
            pstm.setString(7,user.getPhone());

            ret=pstm.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(con,pstm,null);
        }
        return ret;
    }

    @Override
    public T_user denglu(String account, String password) {
        T_user user=null;
        Connection con=getConnection();
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            //sql语句
            String sql="SELECT  * FROM  t_user  WHERE account=? AND password=?";
            pstm=con.prepareStatement(sql);
            pstm.setString(1,account);
            pstm.setString(2,password);
            rs=pstm.executeQuery();
            while (rs.next()){
                user=new T_user(
                        rs.getInt("id"),
                        rs.getString("account"),
                        rs.getString("nick_name"),
                        rs.getString("password"),
                        rs.getInt("gender"),
                        rs.getString("id_card_no"),
                        rs.getString("email"),
                        rs.getString("phone"),
                        rs.getInt("type"),
                        rs.getString("open_id")
                );
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(con,pstm,rs);
        }
        return user;
    }
}
